package com.boxtelecom.adapters;

import java.util.ArrayList;
import java.util.List;

import com.boxtelecom.interfaces.IMyArrayAdapter;
import com.boxtelecom.interfaces.IMyArrayAdapterListener;
import com.boxtelecom.mycircle.R;
import com.boxtelecom.users.FacebookUser;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FriendsListArrayAdapter extends ArrayAdapter<FacebookUser> implements IMyArrayAdapter{
	
	private int _resource;
	private List<IMyArrayAdapterListener> _listeners = null;

	public FriendsListArrayAdapter(Context context, int textViewResourceId,
			List<FacebookUser> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		_resource = textViewResourceId;
	}
	
	public View getView(int position, View convertView, ViewGroup parent){
		LinearLayout layout;
		
		FacebookUser anUser = getItem(position);
						
		if(convertView == null){
			layout = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(inflater);
			vi.inflate(_resource, layout, true);
		} else {
			layout = (LinearLayout) convertView;
		}
		
		TextView nameTextView = (TextView) layout.findViewById(R.id.name);
		
		ImageView photoImageView = (ImageView) layout.findViewById(R.id.photo);
		ImageView signeImageView = (ImageView) layout.findViewById(R.id.logosigne);
		
		nameTextView.setText(anUser.getName());
		nameTextView.setTextColor(anUser.getColorPresence());
		
		// if there is a path for the picture, show it
		// else do not show it
		if(!anUser.isPicturePathNull()){
			photoImageView.setImageBitmap(anUser.getPicture());
			photoImageView.setVisibility(ImageView.VISIBLE);
		} else {
			photoImageView.setVisibility(ImageView.INVISIBLE);
		}
		
		// if the user has a birth sign, show it
		// else do not show it
		if(!(anUser.getSigneAstro().equals("null"))){
			signeImageView.setImageResource(anUser.getIdSigneAstro());
			signeImageView.setVisibility(ImageView.VISIBLE);
		} else {
			signeImageView.setVisibility(ImageView.INVISIBLE);
		}

		// notify that a new message has been received setting the name in italic and added a shadow to it
		if(anUser.isNewMessage()){
			nameTextView.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
			nameTextView.setShadowLayer((float) 2.75, 1, 1, anUser.getColor());
		} else {
			nameTextView.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
			nameTextView.setShadowLayer(0, 1, 1, anUser.getColor());
		}

		nameTextView.setTag(position);
		
		// onClick listener on the name (also on the photo and the birth sign)
		// to obtain the user that has been clicked
		nameTextView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Integer pos = (Integer) v.getTag();
				fireOnClick(getItem(pos));
			}
		});
		
		photoImageView.setTag(position);
		signeImageView.setTag(position);
		
		photoImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Integer pos = (Integer) v.getTag();
				fireOnClick(getItem(pos));
			}
		});
		
		signeImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Integer pos = (Integer) v.getTag();
				fireOnClick(getItem(pos));
			}
		});
		
		return layout;
	}

	@Override
	public void addListener(IMyArrayAdapterListener listener) {
		// TODO Auto-generated method stub
		if(_listeners == null){
			_listeners = new ArrayList<IMyArrayAdapterListener>();
		}
		_listeners.add(listener);
	}

	@Override
	public void removeListener(IMyArrayAdapterListener listener) {
		// TODO Auto-generated method stub
		if(_listeners != null){
			_listeners.remove(listener);
		}
	}
	
	private void fireOnClick(FacebookUser anUser){
		for(IMyArrayAdapterListener listener : _listeners){
			listener.onClickFacebookUser(anUser);
		}
	}
}
